//
//  CellOne.m
//  project-A (pianke)
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 scott. All rights reserved.
//



#import "CellOne.h"
#import "CelloneTab.h"
#import "CelloneTab2.h"
#import "CelloneTab3.h"
#import "CelloneTab4.h"
#import "CelloneTab5.h"
#import "Cellscoll.h"
#import "Model.h"
#import <AFURLSessionManager.h>
#import <UIImageView+WebCache.h>
#import "NetWorkingHandler.h"
#import <DKNightVersion.h>
#define  WIDTH self.frame.size.width

#define HEIGHT self.frame.size.height

@interface CellOne ()

@property (nonatomic,retain)NSMutableArray *arr;

@property (nonatomic,retain)NSMutableArray *arrPic;
@end

@interface CellOne ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation CellOne
-(void)posturl{
    
    
    NSString *str = @"http://api2.pianke.me/pub/today";
  [NetWorkingHandler netWorkingWithURL:str completeHandle:^(NSURLResponse *response, NSData *data, id result) {
      self.arrPic = [NSMutableArray array];
      self.arr = [NSMutableArray array];
      NSMutableArray *arr = [[result objectForKey:@"data"] objectForKey:@"carousel"];
      for (NSDictionary *dic in arr) {
          
          [self.arrPic addObject:[dic objectForKey:@"img"]];
      }
    
      
       NSMutableArray *arrArticle = [[result objectForKey:@"data"] objectForKey:@"list"];
      
      for (NSDictionary *dic in arrArticle) {
          Model *model = [[Model alloc]init];
          [model setValuesForKeysWithDictionary:dic];
          [self.arr addObject:model];
          [model release];
      }
      
      
      [self.tableview reloadData];
  }];

    
    }
     
     
-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.tableview.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        
        self.tableview = [[UITableView alloc]init];
      
        [self.contentView addSubview:self.tableview];
        self.tableview.backgroundColor = [UIColor lightGrayColor];
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
       self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        //早安故事
        
        self.tableview.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        [self.tableview registerClass:[CelloneTab class] forCellReuseIdentifier:@"pool1"];
        //Ting Sound
        [self.tableview registerClass:[CelloneTab2 class] forCellReuseIdentifier:@"pool2"];
        //碎片
        [self.tableview registerClass:[CelloneTab3 class] forCellReuseIdentifier:@"pool3"];
        //音乐 Misic
        [self.tableview registerClass:[CelloneTab4 class] forCellReuseIdentifier:@"pool4"];
        //摄影 photo  插画 lllustration
        [self.tableview registerClass:[CelloneTab5 class] forCellReuseIdentifier:@"pool5"];
        
        [self.tableview registerClass:[Cellscoll class] forCellReuseIdentifier:@"pool0"];
        
        
        [self.tableview release];
        
             
    }
    
    return self;
}
-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    
    self.tableview.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
 
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return self.arr.count + 1;
    
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 0.5 * WIDTH;
    }else{
    Model *model = [self.arr objectAtIndex:indexPath.row -1];
    if (model.type == 2) {
        
        return 345;
    }else if (model.type == 24){
        
        return 380;
    }else if(model.type == 5){
        
        return 310;
    }else if (model.type == 4 ){
        
        return 595;
    }else{
        return 350;
        
        
    }
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 0) {
        Cellscoll *cell = [tableView dequeueReusableCellWithIdentifier:@"pool0"];
        cell.arr = [NSMutableArray arrayWithArray:self.arrPic];
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        cell.layer.masksToBounds = YES;
        
        return cell;
    }else{
  
    Model *model = [self.arr objectAtIndex:indexPath.row - 1];
    if (model.type == 2) {
        CelloneTab2 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        NSString *string = [NSString stringWithFormat:@"%@ · %@",model.name,model.enname];
        cell.labellei.text = string;
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil options:SDWebImageProgressiveDownload];
        cell.labelneirong.text = model.content;
        NSString *str2 = [NSString stringWithFormat:@"%ld",model.like];
        
        
        cell.labelcount.font = [UIFont boldSystemFontOfSize:10];
        cell.labellei.font = [UIFont boldSystemFontOfSize:12];
        cell.labelneirong.font = [UIFont boldSystemFontOfSize:15];
        cell.labelneirong.numberOfLines = 3;
        
        
        cell.labelcount.text = str2;
       
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        cell.layer.masksToBounds = YES;

        return cell;
    }else if (model.type == 24){
        CelloneTab3 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool3"];
       
        NSString *string = [NSString stringWithFormat:@"%@ · %@",model.name,model.enname];
        cell.labelei.text = string;
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil options:SDWebImageProgressiveDownload];
        cell.labelneirong.text = model.content;
        NSString *str2 = [NSString stringWithFormat:@"%ld",model.like];
        
        cell.labelcount.text = str2;
        
        cell.labelbiao.text = model.title;
        
        
        cell.labelcount.font = [UIFont boldSystemFontOfSize:10];
        cell.labelei.font = [UIFont boldSystemFontOfSize:12];
        cell.labelneirong.font = [UIFont boldSystemFontOfSize:15];
        cell.labelneirong.numberOfLines = 3;
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        cell.layer.masksToBounds = YES;

        return cell;
    }else if(model.type == 5){
        CelloneTab4 *cell   = [tableView dequeueReusableCellWithIdentifier:@"pool4"];
        
        NSString *string = [NSString stringWithFormat:@"%@ · %@",model.name,model.enname];
        cell.labellei.text = string;
        
        cell.labelneirong.text = model.content;
        
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil options:SDWebImageProgressiveDownload];
        
        cell.labelsong.text = model.title;
        
        NSString *str2 = [NSString stringWithFormat:@"%ld",model.like];
        
        cell.labelcount.text = str2;

        cell.lableby.text = [model.userinfo objectForKey:@"uname"];
        
        
        cell.labelcount.font = [UIFont boldSystemFontOfSize:10];
        cell.labellei.font = [UIFont boldSystemFontOfSize:12];
        cell.labelneirong.font = [UIFont boldSystemFontOfSize:15];
        cell.labelneirong.numberOfLines = 3;
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        cell.layer.masksToBounds = YES;

        
        return cell;
    }else if (model.type == 4 ){
        CelloneTab5 *cell  = [tableView dequeueReusableCellWithIdentifier:@"pool5"];
        
        NSString *string = [NSString stringWithFormat:@"%@ · %@",model.name,model.enname];
        cell.labellei.text = string;
        [cell.image1 sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil options:SDWebImageProgressiveDownload];
        cell.labeltitle.text = model.title;
        
        NSString *str2 = [NSString stringWithFormat:@"%ld",model.like];
        
        cell.labelcount.text = str2;
        cell.labelneirong.text = model.content;
        cell.labelcount.font = [UIFont boldSystemFontOfSize:10];
        cell.labellei.font = [UIFont boldSystemFontOfSize:12];
        cell.labelneirong.font = [UIFont boldSystemFontOfSize:15];
        cell.labelneirong.numberOfLines = 3;
        
               
        NSString *str = [[model.imglist objectAtIndex:1]objectForKey:@"imgurl"];
       
        [cell.image2 sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil options:SDWebImageProgressiveDownload];
      
        
        NSString *str3 = [[model.imglist objectAtIndex:2]objectForKey:@"imgurl"];
        
        [cell.image3 sd_setImageWithURL:[NSURL URLWithString:str3] placeholderImage:nil options:SDWebImageProgressiveDownload];
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        cell.layer.masksToBounds = YES;

        
        return cell;
    }else{
        CelloneTab *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        NSString *string = [NSString stringWithFormat:@"%@ · %@",model.name,model.enname];
        cell.labelLei.text = string;
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil options:SDWebImageProgressiveDownload];
        cell.labelTitle.text = model.title;
        cell.labelneirong.text = model.content;
        NSString *str2 = [NSString stringWithFormat:@"%ld",model.like];
        
        
        cell.labelTitle.font = [UIFont boldSystemFontOfSize:20];
        cell.labelcount.text = str2;
        cell.labelcount.font = [UIFont boldSystemFontOfSize:10];
        cell.labelLei.font = [UIFont boldSystemFontOfSize:12];
        cell.labelneirong.font = [UIFont boldSystemFontOfSize:15];
        cell.labelneirong.numberOfLines = 3;
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        
        cell.layer.borderWidth = 1;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        cell.layer.masksToBounds = YES;

        
        return cell;
        
             }
      }
   
}

-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    
    return layoutAttributes;
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
    
}


@end
